/*
   
   Copyright 2011 DAVID ALBERTSON, FRANCIS DESLAURIERS, ANTHONY BUFFET  &  DUINOP.COM

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
*/

package com.gpsmap;


import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class map extends Activity implements LocationListener{
	  
	  private static final String MAP_URL = "http://www.incognitoproject.com/duinop/map/index2.html";
	  private WebView webView;
	  private Location mostRecentLocation;
	 

	  @Override
	  /** Called when the activity is first created. */
	  public void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      requestWindowFeature(Window.FEATURE_NO_TITLE);
	      setContentView(R.layout.map);
	      getLocation();
	      setupWebView();
	      this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

	  }
	  /** Sets up the WebView object and loads the URL of the page **/
	  private void setupWebView(){
		  //final String centerURL = "javascript:centerAt(" + 
		  //mostRecentLocation.getLatitude() + "," + 
		  //mostRecentLocation.getLongitude()+ ")";
	    
		  webView = (WebView) findViewById(R.id.webview);
		  webView.clearCache(false);
		  webView.getSettings().setJavaScriptEnabled(true);
		  //Wait for the page to load then send the location information
        
                
        // webview.clearCache(boolean includeDiskFiles) TO ADD TO CLEAR CACHE
        
        
		  webView.setWebViewClient(new WebViewClient(){  
			  @Override  
			  public void onPageFinished(WebView view, String url)  
			  {
				  //webView.loadUrl(centerURL);
			  }

		  });
		  webView.loadUrl(MAP_URL);  




	  }




	  /** The Location Manager manages location providers. This code searches
	        for the best provider of data (GPS, WiFi/cell phone tower lookup,
	        some other mechanism) and finds the last known location.
	   **/
	  private void getLocation() {      
	      LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
	      Criteria criteria = new Criteria();
	      criteria.setAccuracy(Criteria.ACCURACY_FINE);
	      String provider = locationManager.getBestProvider(criteria,true);

	      //In order to make sure the device is getting location, request updates.        locationManager.requestLocationUpdates(provider, 1, 0, this);
	      mostRecentLocation = locationManager.getLastKnownLocation(provider);
	  }

	  /** Sets the mostRecentLocation object to the current location of the device **/
	  @Override
	  public void onLocationChanged(Location location) {
	      mostRecentLocation = location;
	  }

	  /** The following methods are only necessary because WebMapActivity implements LocationListener **/ 
	  @Override
	  public void onProviderDisabled(String provider) {
	  }

	  @Override
	  public void onProviderEnabled(String provider) {
	  }

	  @Override
	  public void onStatusChanged(String provider, int status, Bundle extras) {
	  }

	}